<?php
/**
 * Created by PhpStorm.
 * User: yyc
 * Date: 2018/9/6
 * Time: 9:55
 */
class ASSLoginData extends Eloquent
{
    /**
     * @var   array
     */
    public static $SUPPLIER_WORK_TYPE = [1, 2, 3];    //和供应商合作状态  (1:正常;2:暂停;3:终止;)

    protected $table = "ass_login_data";

    const SUPPLIER_WORK_ON = 1;                        //和供应商合作状态正常

    /**
     * @Description 供应商下拉列表
     * @return mixed
     * @author yyc
     * @date 2018/9/7 9:10
     */
    public static function supplierListPms()
    {
        return MaintenanceSupplier::select('id', 'name')->whereIn('work_together_type', self::$SUPPLIER_WORK_TYPE)->get();
    }

    /**
     * 登录统计
     * @param $page
     * @param $pageSize
     * @param $date
     * @param $id
     * @return mixed
     * @author yyc
     * @date 2018/9/7 11:54
     */
    public static function supplierLoginData($page, $pageSize, $date, $id)
    {
        $selectArray = [
            'maintenance_supplier.id', 'maintenance_supplier.name', 'ass_login_data.login_date',
            'ass_login_data.login_num_day'
        ];
        $result = ASSLoginData::select($selectArray)
            ->leftJoin('maintenance_supplier', 'ass_login_data.pms_supplier_id', '=', 'maintenance_supplier.id')
            ->orderBy('maintenance_supplier.id')
            ->where('maintenance_supplier.work_together_type', self::SUPPLIER_WORK_ON);
        if (!empty($id)) {
            $result->where('maintenance_supplier.id', $id);
        }

        $tmp = clone $result;
        if (!empty($date)) {        //有时间按时间
            $result->where('ass_login_data.login_date', Common::datetime2Num($date));
        } else {                    //没有时间按照本月
            $result->where('ass_login_data.login_date', Common::datetime2Num(date('Y-m', time())));
        }

        //分页总数
        $pageData = clone $result;
        $pageCount = $pageData->get()->toArray();

        //登录次数总数
        $resultAll = $tmp->get()->toArray();

        //分页数据
        $result = $result->groupBy('id')->skip($pageSize * ($page - 1))->take($pageSize)->get()->toArray();



        if (empty($result) || empty($resultAll) || empty($pageCount)) {
            return [
                'result' => $result,
                'count'  => 0,
            ];
        }

        $supplierIdCount = array_unique(array_column($pageCount, 'id'));
        $supplierId = array_unique(array_column($resultAll, 'id'));

        $supplierDataMonth = [];

        //所有按供应商分组
        foreach ($resultAll as $dateValue) {
            if (in_array($dateValue['id'], $supplierId)) {
                $supplierDataMonth[$dateValue['id']][] = $dateValue;
            }
        }

        $numAll = $supplierId;
        //总的登录次数
        foreach ($supplierDataMonth as $dataValue) {
            $num = array_sum(array_unique(array_column($dataValue, 'login_num_day')));
            $id = array_unique(array_column($dataValue, 'id'));
            $numAll[$id[0]] = $num;
        }

        //返回的分页数据 $result  需要调换
        foreach ($result as &$value) {
            $value['num'] = $numAll[$value['id']];
        }

        return [
            'result' => $result,
            'count'  => count($supplierIdCount),
        ];

    }

}